﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace BDSim
{
    /// <summary>
    /// 气象环境仿真
    /// </summary>
    public class ClimateSimModel : SpaceEnvModel
    {
        #region 字段和属性
        /// <summary>
        /// 右键命令集合
        /// </summary>
        private List<MenuCommand> MenuCommandList = new List<MenuCommand>();

        /// <summary>
        /// 气象参数文件
        /// </summary>
        private string _ClimateFile = "bjfs2740.15m";        
        [Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
        [XmlAttribute, Browsable(false), CategoryAttribute("模型设置"), DescriptionAttribute("对流层计算模型气象参数文件路径，如果没有仿真时刻气象文件则采用标准气象参数")]
        public string ClimateFile
        {
            get { return _ClimateFile; }
            set { _ClimateFile = value; }
        }

        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public ClimateSimModel()
        {
            MenuCommand MenuCommand1 = new MenuCommand();
            MenuCommand1.Header = "气象参数";
            //二维显示
            MenuCommand MenuCommand11 = new MenuCommand();
            MenuCommand11.Header = "二维显示";
            //气压二维显示
            MenuCommand MenuCommand111 = new MenuCommand();
            MenuCommand111.Header = "气压";
            MenuCommand111.DefineExcuteCommand(TwoDimensionShowFun);
            //温度二维显示
            MenuCommand MenuCommand112 = new MenuCommand();
            MenuCommand112.Header = "温度";
            MenuCommand112.DefineExcuteCommand(TwoDimensionShowFun);
            //湿度二维显示
            MenuCommand MenuCommand113 = new MenuCommand();
            MenuCommand113.Header = "湿度";
            MenuCommand113.DefineExcuteCommand(TwoDimensionShowFun);
            //导出文件
            MenuCommand MenuCommand12 = new MenuCommand();
            MenuCommand12.Header = "导出文件";
            MenuCommand12.DefineExcuteCommand(ExportFileFun);

            //右键命令组织形式
            MenuCommand11.SubMenuCommandList.Add(MenuCommand111);
            MenuCommand11.SubMenuCommandList.Add(MenuCommand112);
            MenuCommand11.SubMenuCommandList.Add(MenuCommand113);
            MenuCommand1.SubMenuCommandList.Add(MenuCommand11);
            MenuCommand1.SubMenuCommandList.Add(MenuCommand12);
            MenuCommandList.Add(MenuCommand1);
            Menu.ItemsSource = MenuCommandList;
        }

        #endregion

        #region 方法
        /// <summary>
        /// VTEC二维显示函数
        /// </summary>
        /// <param name="param"></param>
        private void TwoDimensionShowFun(object param)
        {
            
        }

        /// <summary>
        /// 气参数导出文件函数
        /// </summary>
        /// <param name="param"></param>
        private void ExportFileFun(object param)
        {
            string filename = currentDirectory + "\\DataRecords\\环境段\\气象参数数据_" +
                DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
            ExportSpaceEnvFile climateParaFile = new ExportSpaceEnvFile(1, filename);
            climateParaFile.ShowDialog();
            if (climateParaFile.SelResult == true)
            {
                Thread ThreadExportFile = new Thread(new ParameterizedThreadStart(ThreadExportFileFun));
                ThreadExportFile.Start(climateParaFile.FileName);
            }
        }

        private void ThreadExportFileFun(object obj)
         {
            UTCTime start = BDSimObject.GlobalStartTime;
            UTCTime end = BDSimObject.GlobalEndTime;

            BLH_PosVel pos = new BLH_PosVel();
            MeteorologicalsParam cpara;

            ClimateWriter climateWriter = new ClimateWriter();
            climateWriter.InitClimateDataFile((string)obj);

            for (UTCTime time = start; time.CompareTo(end) <= 0; time.AddSecond(IntervalTime))
            {
                for (double B = -87.5; B <= 87.5; B = B + IntervalBLH.B) //纬度
                {
                    for (double H = 0.0; H <= 10000.0; H = H + IntervalBLH.H) //高度
                    {
                        pos.B = B;
                        pos.H = H;
                        cpara = GetClimatePara(time, pos, Enabled);
                        climateWriter.WriteFile(time, B, H, cpara.Pressure, cpara.Temperature, cpara.RelHumidity);
                    }
                }
            }
            MessageBox.Show("导出气象参数完成", "提示");
        }

        /// <summary>
        /// 获取气象参数
        /// </summary>
        /// <param name="time"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static MeteorologicalsParam GetClimatePara(UTCTime time, BLH_PosVel pos, bool enabled)
        {
            MeteorologicalsParam paraOut = new MeteorologicalsParam();
            paraOut.Temperature = 15;
            paraOut.Pressure = 1013.25;
            paraOut.RelHumidity = 50;
            paraOut.SatVaporPressure = 0.00204;
            paraOut.D_Trop = -6.81;
            paraOut.LeanVaporPressure = 0.00204;

            if (enabled)
            {
                ClimateParaInput paraIn;
                paraIn.time = time;     //时间
                paraIn.Lat = pos.B;     //纬度
                paraIn.High = pos.H;    //高度
                BDSim.ClimateParaDLL.CP_GetClimatePara(paraIn, out paraOut);
            }

            return paraOut;
        }

        public static double[,] xxxx()
        {
            double[,] data = new double[72, 72];
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                }
            }
            return data;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public override bool Init()
        {
            return false;
        }

        #endregion
    }
}
